# LarkShot 双版本构建脚本 (PowerShell)
param(
    [switch]$Quick,
    [switch]$SelfContainedOnly,
    [switch]$FrameworkDependentOnly
)

Write-Host "===============================================" -ForegroundColor Cyan
Write-Host "         LarkShot 双版本构建脚本" -ForegroundColor Cyan
Write-Host "===============================================" -ForegroundColor Cyan
Write-Host

# 清理输出目录
Write-Host "清理之前的构建输出..." -ForegroundColor Yellow
if (Test-Path "bin\Release") {
    Remove-Item "bin\Release" -Recurse -Force
}
Write-Host

$buildSelfContained = $true
$buildFrameworkDependent = $true

if ($SelfContainedOnly) {
    $buildFrameworkDependent = $false
}
if ($FrameworkDependentOnly) {
    $buildSelfContained = $false
}
if ($Quick) {
    $buildFrameworkDependent = $false
}

$results = @()

# 构建自包含版本
if ($buildSelfContained) {
    Write-Host "[1/2] 正在构建自包含版本（无需运行时，体积较大）..." -ForegroundColor Green
    Write-Host "----------------------------------------------" -ForegroundColor Gray
    
    Start-Process -FilePath "dotnet" -ArgumentList @(
        "publish", "-c", "Release", "-r", "win-x64", 
        "--self-contained", "true", 
        "-p:PublishSingleFile=true", 
        "-p:PublishTrimmed=false", 
        "-p:IncludeNativeLibrariesForSelfExtract=true", 
        "-o", "bin\Release\SelfContained"
    ) -Wait -NoNewWindow | Out-Null
    
    $selfContainedPath = "bin\Release\SelfContained\LarkShot.exe"
    if (Test-Path $selfContainedPath) {
        $size = (Get-Item $selfContainedPath).Length
        $results += @{
            Type = "自包含版本"
            Success = $true
            Path = $selfContainedPath
            Size = $size
            SizeMB = [math]::Round($size / 1MB, 2)
        }
    } else {
        $results += @{
            Type = "自包含版本"
            Success = $false
        }
    }
    Write-Host
}

# 构建依赖运行时版本
if ($buildFrameworkDependent) {
    Write-Host "[2/2] 正在构建依赖运行时版本（需要.NET运行时，体积较小）..." -ForegroundColor Green
    Write-Host "----------------------------------------------" -ForegroundColor Gray
    
    Start-Process -FilePath "dotnet" -ArgumentList @(
        "publish", "-c", "Release", "-r", "win-x64", 
        "--self-contained", "false", 
        "-p:PublishSingleFile=true", 
        "-o", "bin\Release\FrameworkDependent"
    ) -Wait -NoNewWindow | Out-Null
    
    $frameworkDependentPath = "bin\Release\FrameworkDependent\LarkShot.exe"
    if (Test-Path $frameworkDependentPath) {
        $size = (Get-Item $frameworkDependentPath).Length
        $results += @{
            Type = "依赖运行时版本"
            Success = $true
            Path = $frameworkDependentPath
            Size = $size
            SizeMB = [math]::Round($size / 1MB, 2)
        }
    } else {
        $results += @{
            Type = "依赖运行时版本"
            Success = $false
        }
    }
    Write-Host
}

# 显示构建结果
Write-Host "===============================================" -ForegroundColor Cyan
Write-Host "               构建结果检查" -ForegroundColor Cyan
Write-Host "===============================================" -ForegroundColor Cyan

foreach ($result in $results) {
    if ($result.Success) {
        Write-Host "✓ $($result.Type)构建成功！" -ForegroundColor Green
        Write-Host "  位置: $($result.Path)" -ForegroundColor Gray
        Write-Host "  大小: $($result.SizeMB) MB ($($result.Size.ToString('N0')) 字节)" -ForegroundColor Gray
    } else {
        Write-Host "✗ $($result.Type)构建失败！" -ForegroundColor Red
    }
    Write-Host
}

if ($results.Count -gt 1) {
    $selfContained = $results | Where-Object { $_.Type -eq "自包含版本" -and $_.Success }
    $frameworkDependent = $results | Where-Object { $_.Type -eq "依赖运行时版本" -and $_.Success }
    
    if ($selfContained -and $frameworkDependent) {
        $sizeReduction = [math]::Round((1 - $frameworkDependent.Size / $selfContained.Size) * 100, 1)
        Write-Host "📊 体积对比: 依赖运行时版本比自包含版本小 $sizeReduction%" -ForegroundColor Magenta
        Write-Host
    }
}

Write-Host "===============================================" -ForegroundColor Cyan
Write-Host "                使用说明" -ForegroundColor Cyan
Write-Host "===============================================" -ForegroundColor Cyan

$selfContainedResult = $results | Where-Object { $_.Type -eq "自包含版本" -and $_.Success }
if ($selfContainedResult) {
    Write-Host "自包含版本 ($($selfContainedResult.Path)):" -ForegroundColor Yellow
    Write-Host "  - 无需安装.NET运行时" -ForegroundColor Gray
    Write-Host "  - 文件体积较大（$($selfContainedResult.SizeMB) MB）" -ForegroundColor Gray
    Write-Host "  - 可在任何Windows系统上直接运行" -ForegroundColor Gray
    Write-Host
}

$frameworkDependentResult = $results | Where-Object { $_.Type -eq "依赖运行时版本" -and $_.Success }
if ($frameworkDependentResult) {
    Write-Host "依赖运行时版本 ($($frameworkDependentResult.Path)):" -ForegroundColor Yellow
    Write-Host "  - 需要安装.NET 8运行时" -ForegroundColor Gray
    Write-Host "  - 文件体积较小（$($frameworkDependentResult.SizeMB) MB）" -ForegroundColor Gray
    Write-Host "  - 目标机器需安装.NET 8.0运行时" -ForegroundColor Gray
    Write-Host "  - 运行时下载地址: https://dotnet.microsoft.com/download/dotnet/8.0" -ForegroundColor Cyan
    Write-Host
}

Write-Host "重要提示：" -ForegroundColor Red
Write-Host "1. 程序需要管理员权限运行（已配置UAC）" -ForegroundColor White
Write-Host "2. 目标程序已硬编码为 GateMK-Win64-Shipping.exe" -ForegroundColor White
Write-Host "3. 默认快捷键为 PrintScreen" -ForegroundColor White
Write-Host "4. 程序会自动最小化到系统托盘" -ForegroundColor White
Write-Host "===============================================" -ForegroundColor Cyan

Write-Host
Write-Host "构建脚本使用方法：" -ForegroundColor Magenta
Write-Host "  .\build.ps1                    # 构建两个版本" -ForegroundColor Gray
Write-Host "  .\build.ps1 -Quick             # 快速构建（仅自包含版本）" -ForegroundColor Gray
Write-Host "  .\build.ps1 -SelfContainedOnly # 仅构建自包含版本" -ForegroundColor Gray
Write-Host "  .\build.ps1 -FrameworkDependentOnly # 仅构建依赖运行时版本" -ForegroundColor Gray

Write-Host
Write-Host "按任意键退出..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
